package leetcode.solution20;

import java.util.Stack;

public class Main20 {
    public static boolean isValid(String s) {
        char[] strs = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for(char c : strs){
            if(c == '(' || c == '[' || c == '{'){
                stack.push(c);
            }else {
                //先判断非空
                if(stack.isEmpty()){
                   return false;
                }
                if(c == ')' && stack.pop() != '('){
                    return false;
                }
                if(c == ']' && stack.pop() != '['){
                    return false;
                }
                if(c == '}' && stack.pop() != '{'){
                    return false;
                }
            }
        }
        if(stack.isEmpty()){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String s = "}";
        boolean valid = isValid(s);
        System.out.println(valid);
    }
}
